package com.lc.user.express;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.user.express.app.AppContext;


/**
 * @author Administrator
 * @ClassName: BaseFragment
 * @Function: TODO fragment基类 用法和注释详见BaseSecondActivity 标题栏 继承后获得标题栏样式    左：返回  中：标题 右：文字或图片按钮
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    protected MainActivity activity;
    public View mContentView;
    private TextView tv_root_title;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.base_title_fragement, container, false);
        tv_root_title = (TextView) view.findViewById(R.id.tv_root_title);
        LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent);
        mContentView = getContentView(inflater, container, savedInstanceState);
        if (null == mContentView) {
            return null;
        }
        AppContext.ScaleScreenHelper.loadView((ViewGroup) mContentView);
        llContent.addView(mContentView);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) view);
        return view;
    }

    public abstract View getContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    public void show(Class<?> cls, int flags, boolean isStartActivityForResult, int requestCode, Bundle bundle) {
        Intent i = new Intent(activity, cls);
        if (flags != -1) {
            i.addFlags(flags);
        }
        if (null != bundle) {
            i.putExtras(bundle);
        }
        if (isStartActivityForResult) {
            startActivityForResult(i, requestCode);
        } else {
            startActivity(i);
        }
    }

    public void show(Class<?> cls, int flags, boolean isStartActivityForResult, int requestCode) {
        show(cls, flags, isStartActivityForResult, requestCode, null);
    }

    public void show(Class<?> cls, int flags) {
        show(cls, flags, false, -1);
    }

    public void show(Class<?> cls) {
        show(cls, -1, false, -1);
    }

    public void show(Class<?> cls, Bundle bundle) {
        show(cls, -1, false, -1, bundle);
    }

    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (activity.getCurrentFocus() != null) {
            ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                    activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = (MainActivity) activity;
        super.onAttach(activity);
    }

    public void setTitle(CharSequence title) {
        tv_root_title.setText(title);
    }

    public void setHeadLayoutVisibility(int visibility) {
        tv_root_title.setVisibility(visibility);
    }
}

